package com.chuanyang.driverh.mainmy;


import android.content.Context;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.mainmy.adapter.TranslateComAdapter;
import com.chuanyang.driverh.responseresult.LogisticsCompanyData;
import com.chuanyang.driverh.responseresult.LogisticsCompanyResult;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.StringUtil;
import com.chuanyang.driverh.utils.eventbus.SelectCompanyEvent;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;
import okhttp3.Call;

/**
 * 西风多少恨，吹不散眉弯！—唐大帅
 *
 * @author 唐帅
 *         认证和上传图片
 *         created at
 */

public class TranslateCompanyActivity extends BaseTitleActivity implements AdapterView.OnItemClickListener{

    private EditText et_search_company;
    private ListView lv_searchcomany_list;
    private TranslateComAdapter adapter;
    private ArrayList<LogisticsCompanyData> list;
    @Override
    protected int getContentResId(){
        return R.layout.my_act_search;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        et_search_company = (EditText) findViewById(R.id.et_search_driver);
        lv_searchcomany_list = (ListView) findViewById(R.id.lv_searchcaream_list);
    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("物流公司");
        getRightTextView().setText("搜索");
        list = new ArrayList();
        adapter = new TranslateComAdapter(this, list);
        lv_searchcomany_list.setAdapter(adapter);
        selectLgCompanyByName("");
    }

    @Override
    protected void initListener() {
        et_search_company.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId,
                                          KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_UNSPECIFIED || actionId == EditorInfo.IME_ACTION_SEARCH) {
                    /*隐藏软键盘*/
                    InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (inputMethodManager.isActive()) {
                        inputMethodManager.hideSoftInputFromWindow(v.getApplicationWindowToken(), 0);
                    }
                    selectLgCompanyByName(et_search_company.getText().toString());
                    return true;
                }
                return false;
            }
        });
        lv_searchcomany_list.setOnItemClickListener(this );

    }

    private void selectLgCompanyByName(String s) {
        DialogUtil.showLoadingDialog(activityContext,null);
        Map<String,String> params=new HashMap<String, String>();
        params.put("logisticsCompanyName",s);
        MyHttpUtil.selectLgCompanyByName(activityContext,params, new MyCallback<LogisticsCompanyResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(LogisticsCompanyResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    list = result.getData();
                    adapter.setList(list);
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });
    }

    /**
     * 右侧的按钮事件
     */
    protected void baseGoEnsure() {
        String input=et_search_company.getText().toString().trim();
        if (!StringUtil.isBlank(input)){
            selectLgCompanyByName(input);
        }
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        EventBus.getDefault().post(new SelectCompanyEvent(list.get(position)));
        finish();
    }
}
